package org.teamh.artificialIntelligence;

import java.util.HashMap;

import org.teamh.logic.datamodel.Cell;
import org.teamh.logic.datamodel.DatamodelManager;
import org.teamh.logic.datamodel.Waypoint;

public class LoadWorld {
	
	private static final int ROTATE = 45;
	
	private LoadWorld() {}
	
	public static void load(DatamodelManager datamodelManager) {
		countingWorld(datamodelManager);
		rotateCreep(datamodelManager);
	}
	
	private static void countingWorld(DatamodelManager model) {
		HashMap<String, Cell> cells = model.getCells();
		for (Cell cell : cells.values()) {
			cell.setWorldX(cell.getX());
			cell.setWorldY(cell.getY());
		}
	}

	private static void rotateCreep(DatamodelManager model) {
		float current_x = 0;
		float current_y = 0;
		float next_x = 0;
		float next_y = 0;
		HashMap<String, Waypoint> points = model.getWaypoints();
		for (Waypoint point : points.values()) {
			current_x = point.getCell().getWorldX();
			current_y = point.getCell().getWorldY();
			try {
				next_x = point.getNext().getCell().getWorldX();
				next_y = point.getNext().getCell().getWorldY();

				float distance = (float)Math.sqrt(Math.pow(next_x - current_x, 2) + Math.pow(next_y - current_y, 2));
//				float distance = Math.abs(next_x - current_x) + Math.abs(next_y - current_y);
				
				point.setWayToPoint(distance);
				
				if (current_x > next_x && current_y == next_y) {
					point.setRotateUnit(2 * ROTATE);
					continue;
				}
				if (current_x > next_x && current_y < next_y) {
					point.setRotateUnit(3 * ROTATE);
					continue;
				}
				if (current_x == next_x && current_y < next_y) {
					point.setRotateUnit(4 * ROTATE);
					continue;
				}
				if (current_x < next_x && current_y < next_y) {
					point.setRotateUnit(5 * ROTATE);
					continue;
				}
				if (current_x < next_x && current_y == next_y) {
					point.setRotateUnit(6 * ROTATE);
					continue;
				}
				if (current_x < next_x && current_y > next_y) {
					point.setRotateUnit(7 * ROTATE);
					continue;
				}
				if (current_x == next_x && current_y > next_y) {
					point.setRotateUnit(0);
					continue;
				}
				if (current_x > next_x && current_y > next_y) {
					point.setRotateUnit(ROTATE);
					continue;
				}
			} catch (Exception e) {

			}
		}
	}
}
